package com.cskaoyan.shopping.service.impl;

import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement;
import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.converter.CcProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelContentItemDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.dto.CcProductDetailDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Set;

/**
 * @auther cskaoyan
 * @date 2022/8/24:14:59
 */
@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    CcProductConverter iProductConverter;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Override
    public CcProductDetailDto getProductDetail(Long id) {

         //去tb_item  去查其他数据

        Item item = itemMapper.selectByPrimaryKey(id);
        CcProductDetailDto productDetailDto = iProductConverter.productDoToDto(item);

        String[] images = item.getImages();
        productDetailDto.setProductImageSmall(images);
        productDetailDto.setProductImageBig(images[0]);


        //去tb_desc 拿desc
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
        productDetailDto.setDetail(itemDesc.getItemDesc());


        return productDetailDto;
    }

    @Override
    public AllProductResponse getAllProduct(PageRequest pageInfo) {
        // PageHelper.startPage(pageInfo.getPage(),pageInfo.getSize());
        Page<Item> page = PageHelper.startPage(pageInfo.getPage(), pageInfo.getSize());
        Example example = new Example(Item.class);



        //sort = 1  按照价格大小顺序
        if (pageInfo.getSort().equals("1")){
             example.setOrderByClause("price ASC");
        }
        //sort = -1  按照价格倒叙顺序
        if (pageInfo.getSort().equals("-1")){
            example.setOrderByClause("price DESC");
        }

        Example.Criteria criteria = example.createCriteria();

        if (pageInfo.getPriceGt() != null && pageInfo.getPriceLte() != null) {
            criteria.andBetween("price", pageInfo.getPriceGt(), pageInfo.getPriceLte());
        }

        //只输入价格最小值
        if (pageInfo.getPriceGt() != null && pageInfo.getPriceLte() == null) {
            criteria.andGreaterThan("price",pageInfo.getPriceGt());
        }


        //只输入价格最大值
        if (pageInfo.getPriceGt() == null && pageInfo.getPriceLte() != null) {
            criteria.andLessThan("price",pageInfo.getPriceLte());
        }


        //将item 转化为 ProductDto
        List<Item> items = itemMapper.selectByExample(example);

        List<ProductDto> ProductDtos = iProductConverter.itemsToProductDtos(items);

        //循环给 第一张图  因为item是给的一组图
        int count = 0;
        for (ProductDto dto : ProductDtos) {
            dto.setPicUrl(items.get(count).getImageBig());
            count++;
        }

        AllProductResponse allProductResponse = new AllProductResponse();
        // allProductResponse.setData(pfo);
        allProductResponse.setData(ProductDtos);
        allProductResponse.setTotal(page.getTotal());
        return allProductResponse;
    }




    @Override
    public PanelDto getRecommendGoods() {

        Example example = new Example(Panel.class);
        example.createCriteria()
                .andEqualTo("id",6);
        List<Panel> panels = panelMapper.selectByExample(example);

        List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(6);

        PanelDto panelDto = iProductConverter.panelTOPanelDto(panels.get(0));

        //contentItem -> dto

        List<PanelContentItemDto> panelContentItemDtos = iProductConverter.PanelContentsToDtos(panelContentItems);

        // //productName  salePrice subTitle 没有值
        // for (PanelContentItemDto itemDto : panelContentItemDtos) {
        //
        //     Item item = itemMapper.selectByPrimaryKey(itemDto.getId());
        //
        //     itemDto.setProductName(item.getTitle());
        //     itemDto.setSalePrice(item.getPrice());
        //     itemDto.setSubTitle(item.getSell_point());
        //
        // }

        panelDto.setPanelContentItems(panelContentItemDtos);
        return panelDto;
    }

    @Override
    public List<ItemDto> getAllItems() {
        return null;
    }
}
